package com.campsys.controller;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.core.Result;
import com.campsys.core.TableData;
import com.campsys.domain.TManagerActivity;
import com.campsys.domain.TManagerClass;
import com.campsys.domain.TManagerTeacher;
import com.campsys.domain.param.*;
import com.campsys.domain.vo.ClazzListVo;
import com.campsys.domain.vo.ClazzPageVo;
import com.campsys.domain.vo.TeacherListVo;
import com.campsys.mapping.ActivityMapping;
import com.campsys.mapping.ClazzMapping;
import com.campsys.mapping.TeacherMapping;
import com.campsys.service.IClazzService;
import com.campsys.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "教学班管理")
@RestController
@RequestMapping("/clazz")
public class ClazzController {

    @Autowired
    private IClazzService clazzService;

    @ApiOperation("添加教学班")
    @PreAuthorize("@lcc.myAuthority('clazz:add')")
    @PostMapping("/add")
    public Result addClazz(@Validated @RequestBody ClazzAddParam clazzAddParam){

        TManagerClass clazz = ClazzMapping.INSTANCE.to(clazzAddParam);
        boolean rs = clazzService.save(clazz);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("教学班关联老师")
    @PreAuthorize("@lcc.myAuthority('clazz:related:teacher')")
    @PutMapping("/related/teacher")
    public Result relatedTeacher(Long clazzId , Long teacherId){

        System.out.println(clazzId);
        System.out.println(teacherId);
        TManagerClass clazz = new TManagerClass();
        clazz.setId(clazzId);
        clazz.setTeacherId(teacherId);
        boolean rs = clazzService.updateById(clazz);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("教学班关联活动")
    @PreAuthorize("@lcc.myAuthority('clazz:related:activity')")
    @PutMapping("/related/activity")
    public Result relatedActivity(Long clazzId , Long activityId){

        TManagerClass clazz = new TManagerClass();
        clazz.setId(clazzId);
        clazz.setActivityId(activityId);
        boolean rs = clazzService.updateById(clazz);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("修改教学班")
    @PreAuthorize("@lcc.myAuthority('clazz:update')")
    @PutMapping("/update")
    public Result relatedActivity(@Validated @RequestBody ClazzUpdateParam clazzUpdateParam){

        TManagerClass clazz = ClazzMapping.INSTANCE.to(clazzUpdateParam);
        boolean rs = clazzService.updateById(clazz);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("教学班分页查询")
    @PreAuthorize("@lcc.myAuthority('clazz:page')")
    @GetMapping("/page")
    public TableData<ClazzPageVo> getClazzPage(@Validated PageParam pageParam,String name,Long activityId,Long teacherId){

        return clazzService.getClazzPageVo(pageParam, name, activityId, teacherId);
    }

    @ApiOperation("教学班删除")
    @PreAuthorize("@lcc.myAuthority('clazz:delete')")
    @PutMapping("/delete/{id}")
    public Result getClazzPage(@PathVariable Long id){

        TManagerClass tManagerClass = new TManagerClass();
        tManagerClass.setId(id);
        tManagerClass.setStatus(2l);

        boolean rs = clazzService.updateById(tManagerClass);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("教学班添加学生")
    @PreAuthorize("@lcc.myAuthority('clazz:student:add')")
    @PostMapping("/student/add")
    public Result addStudentInClazz(@RequestBody ClazzAddStudentParam clazzAddStudentParam){

        boolean rs = clazzService.addStudentInClazz(clazzAddStudentParam.getClazzId(),clazzAddStudentParam.getStudentIds());

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("教学班删除学生")
    @PreAuthorize("@lcc.myAuthority('clazz:student:delete')")
    @DeleteMapping("/student/delete")
    public Result deleteStudentInClazz(Long studentId, Long clazzId){

        boolean rs = clazzService.deleteStudentInClazz(clazzId,studentId);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("查询教学班列表")
    @PreAuthorize("@lcc.myAuthority('clazz:list')")
    @GetMapping("/list")
    public Result getTeacherList(Long teacherId,Long studentId){

        LambdaQueryWrapper<TManagerClass> qw = new LambdaQueryWrapper<>();
        qw.eq(TManagerClass::getStatus,1l);
        qw.eq(teacherId != null,TManagerClass::getTeacherId,teacherId);
        List<Long> clazzIds = clazzService.getClazzIdsByStudentId(studentId);
        if (studentId != null && (clazzIds == null || clazzIds.isEmpty())) {
            // 如果 studentIds 为空，则返回空的分页结果
            return Result.success();
        }

        List<ClazzListVo> list = ClazzMapping.INSTANCE.toList(clazzService.list(qw));

        return Result.success(list);
    }



}
